package com.ams.studio.xml;


import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zhongyl on 2017-08-13.
 */
class Util {

    public static String parseDate(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    public static Date parseTime(String timeStr, String pattern) {
        if (StringUtils.isEmpty(timeStr)) {
            return null;
        }
        try {
            return DateUtils.parseDate(timeStr, new String[]{pattern});
        } catch (ParseException e) {
            throw new RuntimeException("时间转换错误");
        }
    }

    public static String decimalToString(BigDecimal value, int scale) {
        String format = (scale == 0 ? "0" : "0." + StringUtils.rightPad("", scale, '0'));
        return new DecimalFormat(format).format(value);
    }
}
